<template>
  <view class="container screen-width">
    <u-tabs :list="tabList" :current="current" height="100" item-width="274" @change="handleChange"></u-tabs>
    <scroll-view class="list" :scroll-y="true" @scrolltolower="fetchData()">
      <block v-if="current == 0">
        <view
          class="item u-flex u-row-between"
          v-for="(item, index) in list"
          :key="item.departmentId"
        >
          <view>
            <view class="title" @tap="toPage(item.departmentId)">
              {{
              item.departmentName
              }}
            </view>
            <view class="date">{{ item.createTime }}</view>
          </view>
          <view>
            <view
              class="sub-cancel"
              @tap="isSubscription(index, item.delFlag, item.departmentId)"
            >{{ item.delFlag == 1 ? "恢复订阅" : "已订阅" }}</view>
          </view>
        </view>
      </block>
      <block v-else>
        <view class="item" v-for="(item, index) in list" :key="index">
          <view class="flex items-middle">
            <u-image class="logo" :src="$imgUrl + item.logo" width="160" height="100"></u-image>
            <view @tap="toCompany(item.companyId)">
              <view class="title">{{ item.companyName }}</view>
              <view class="date">{{ item.createTime }}</view>
            </view>
          </view>
          <view class="flex flex-end items-middle">
            <view
              class="sub-cancel"
              @tap="isCompany(index, item)"
            >{{ item.delFlag == 1 ? "恢复订阅" : "已订阅" }}</view>
            <view class="sub-cancel" @click="toPatentPay(item)">专利缴费</view>
          </view>
        </view>
      </block>
      <u-loadmore
        bg-color="#ffffff"
        style="width: 100%;  margin-top: 15rpx;  padding-right: 30rpx"
        :status="status"
      />
    </scroll-view>
    <Festival></Festival>
  </view>
</template>

<script>
import {
  deptFollowList,
  comFollowList,
  companyAttention,
  companyAttentionCancel
} from "../../util/api/user";
export default {
  data() {
    return {
      current: 0,
      list: [],
      tabList: [
        {
          name: "订阅的部门",
          count: 5
        },
        {
          name: "关注的企业",
          count: 0
        }
      ],
      params: {
        pageNo: 1,
        pageSize: 15
      },
      status: "loadmore"
    };
  },
  onLoad() {
    this.fetchData();
  },
  methods: {
    // 点击订阅
    isSubscription(i, val, id) {
      let _this = this;
      if (val == 0) {
        // 取消订阅
        let obj = {
          departmentId: id
        };

        uni.showModal({
          title: "提示",
          content: "您确认要取消订阅吗,取消后将不再接收该部门的政策推送",
          cancelText: "考虑一下",
          success: function(res) {
            if (res.confirm) {
              _this
                .$post("/bmj-api/api/home/cancelAttention?departmentId=" + id)
                .then(res => {
                  // console.log(res);
                  if (res.code == 200) {
                    uni.showToast({
                      title: "取消成功",
                      duration: 2000,
                      icon: "none"
                    });
                    _this.list[i].delFlag = 1;
                  }
                });
            }
          }
        });
      } else {
        // 订阅

        _this
          .$post("/bmj-api/api/home/departmentAttention?departmentId=" + id)
          .then(res => {
            // console.log(res);
            if (res.code == 200) {
              uni.showToast({
                title: "订阅成功",
                duration: 2000,
                icon: "none"
              });
              _this.list[i].delFlag = 0;
            }
          });
      }
    },
    handleChange(index) {
      this.current = index;
      this.fetchData(true);
    },
    getPromise() {
      switch (this.current) {
        case 0:
          return deptFollowList(this.params);
        case 1:
          return comFollowList(this.params);
      }
    },
    fetchData(reset = false) {
      if (reset) {
        this.resetPagination();
      }
      uni.showLoading();
      this.status = "loading";
      this.getPromise().then(res => {
        uni.hideLoading();
        this.$nextTick(() => {
          let records = res.result.records;
          // uni.stopPullDownRefresh();
          // console.log(records);
          this.tabList[this.current]["count"] = res.result.total;
          if (!records.length) {
            this.status = "nomore";
            return;
          }
          this.list = reset ? records : this.list.concat(records);
          this.params.pageNo += 1;
          this.status = "loadmore";
        });
      });
    },
    resetPagination() {
      this.params.pageNo = 1;
      this.list = [];
      this.status = "loadmore";
    },
    // 跳转页面
    toPage(id) {
      console.log(123);
    },
    // 跳转到企业详情
    toCompany(id) {
      uni.navigateTo({
        url: `/pages/enterpriseDetails/brand_detail?id=${id}&type=1&liIndex=0&showNew=true`
      });
    },
    // 公司关注
    isCompany(index, item) {
      if (item.delFlag == 1) {
        companyAttention(item.companyId).then(res => {
          if (res.code === 200) {
            uni.showToast({
              title: "关注成功",
              duration: 2000,
              icon: "none"
            });
            this.list[index].delFlag = 0;
          } else if (res.code === 10004) {
            uni.showModal({
              title: "提示",
              content: "你的关注数量已满，请升级会员解锁更多",
              confirmText: "升级会员",
              success: function(res) {
                if (res.confirm) {
                  // 升级会员
                  uni.navigateTo({
                    url: "/pages/VIP/index"
                  });
                }
              }
            });
          } else {
            uni.showToast({
              title: res.message,
              duration: 2000,
              icon: "none"
            });
          }
        });
      } else {
        companyAttentionCancel(item.companyId).then(res => {
          if (res.code === 200) {
            uni.showToast({
              title: "取消关注成功",
              duration: 2000,
              icon: "none"
            });
            this.list[index].delFlag = 1;
          } else {
            uni.showToast({
              title: res.message,
              duration: 2000,
              icon: "none"
            });
          }
        });
      }
    },
    // 专利缴费
    toPatentPay(item) {
      uni.navigateTo({
        url: `/pages/patent/patentList/Index?id=${item.companyId}&name=${item.companyName}`
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.container {
  height: 100%;
  max-height: 100%;

  .list {
    height: calc(100% - 100rpx);
    width: 100%;
    background: #fff;
    padding: 0 30rpx;
    padding-bottom: 120rpx;

    .item {
      height: 200rpx;
      border-bottom: 1rpx solid #ebebeb;
      padding-top: 20upx;
      box-sizing: border-box;
      .title {
        font-size: 28rpx;
        font-family: PingFang SC, PingFang SC-Regular;
        font-weight: 400;
        text-align: left;
        color: #333333;
      }

      .logo {
        margin-right: 70rpx;
      }
      .date {
        font-size: 22rpx;
        font-family: PingFang SC, PingFang SC-Regular;
        font-weight: 400;
        text-align: left;
        color: #aaaaaa;
      }

      .sub-cancel {
        width: 120rpx;
        height: 48rpx;
        opacity: 1;
        background: #e8f3ff;
        border-radius: 8rpx;
        color: #1678ff;
        line-height: 48rpx;
        text-align: center;
        margin-left: 20upx;
      }
    }
  }
}
</style>
